package com.mygameframework.sprite;

import com.mygameframework.sprite.state.CommonState;
import com.mygameframework.sprite.state.HeroState;
import com.mygameframework.states.IState;
import com.mygameframework.states.StateMachine;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Sprite extends BaseGameEntity {
    private StateMachine<Sprite> stateMachine;

//    private Animation anim;   //改了,原是Map<IState,Animation>
    private Map<Class<? extends IState>,Animation> anims;

    // position (pixels)
    private float x;
    private float y;
    // velocity (pixels per millisecond)
    private float dx;
    private float dy;
    
    private int w,h;
    private Rectangle2D bound;
    /**
        Creates a new Sprite object with the specified Animation.
    */
    public Sprite(Map<Class<? extends IState> ,Animation> anims) {
        this.anims = anims;
        stateMachine=new StateMachine<Sprite>(this);
    }

    /**
        Updates this Sprite's Animation and its position based
        on the velocity.
    */
    public void update(long elapsedTime) {
//        x += dx * elapsedTime;
//        y += dy * elapsedTime;
//        anim.update(elapsedTime);
        stateMachine.update(elapsedTime);
        //TODO 下面句要归入'状态类'的excute()方法里吗?
        //取得当前state对应的动画实例,把动画实例更新()
        Animation anim=anims.get(stateMachine.getCurrentState().getClass());
        if(anim!=null)
            anim.update(elapsedTime);
    }

    /**
        Gets this Sprite's current x position.
    */
    public float getX() {
        return x;
    }

    /**
        Gets this Sprite's current y position.
    */
    public float getY() {
        return y;
    }

    /**
        Sets this Sprite's current x position.
    */
    public void setX(float x) {
        this.x = x;
    }

    /**
        Sets this Sprite's current y position.
    */
    public void setY(float y) {
        this.y = y;
    }

    public Rectangle2D getBound() {
        return bound;
    }
    
    protected Animation getCurrentAnimation(){
        return anims.get(stateMachine.getCurrentState().getClass());
    }
    public  Animation getAnimation(Class<? extends IState> clazz){
        return anims.get(clazz);
    }
    /**
        Gets this Sprite's width, based on the size of the
        current image.
    */
    public int getWidth() {
        return w;
    }

    /**
        Gets this Sprite's height, based on the size of the
        current image.
    */
    public int getHeight() {
        return h;
    }

    /**
        Gets the horizontal velocity of this Sprite in pixels
        per millisecond.
    */
    public float getVelocityX() {
        return dx;
    }

    /**
        Gets the vertical velocity of this Sprite in pixels
        per millisecond.
    */
    public float getVelocityY() {
        return dy;
    }

    /**
        Sets the horizontal velocity of this Sprite in pixels
        per millisecond.
    */
    public void setVelocityX(float dx) {
        this.dx = dx;
    }

    /**
        Sets the vertical velocity of this Sprite in pixels
        per millisecond.
    */
    public void setVelocityY(float dy) {
        this.dy = dy;
    }

    /**
        Gets this Sprite's current image.
    */
    public Image getImage() {
        //TODO 改,根据状态回返图帧 已改
//        return anims.get(stateMachine.getCurrentState().getClass()).getImage();
        return null;
    }
    public boolean collidesWith(Sprite other){
        return (bound.contains(other.getBound()) || bound.intersects(other.getBound()));
    }
    public static void main(String[] args) {
        IState hero=new CommonState();
        System.out.println(hero.getClass());
         Map<Class<? extends IState>,String> anims=
                 new HashMap<Class<? extends IState>, String>();
         String s="英雄";
         Class<? super CommonState> clazz;
         clazz=IState.class;
         anims.put(hero.getClass(),s);
         System.out.println(anims.get(hero.getClass()));
         
         CommonState hero1=new CommonState();
         
         
    }

    @Override
    public void render(Graphics2D g) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
